<template>
  <div style="margin-top: 10px; text-align: right">
    这里会有一个组件{{ roleId }}
    <el-tree
      ref="tree"
      :data="list"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <el-button type="primary" @click="hSubmit">确定</el-button>
    <el-button>取消</el-button>
  </div>
</template>

<script>
import { getPermissionList } from "@/api/permission";
import { tranListToTreeData } from "@/utils/index";
import { getRoleDetail, assignPerm } from "@/api/setting";
export default {
  props: {
    roleId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.loadPermissionList();
    this.loadPermissionById();
  },
  methods: {
    // 通知父组件去取消弹层
    async hSubmit() {
      // 组装参数
      const permIds = this.$refs.tree.getCheckedKeys();
      console.log(permIds);
      // 调用api
      await assignPerm({ id: this.roleId, permIds: permIds });
      // 关闭弹窗
      this.$emit("success");
    },
    async loadPermissionList() {
      const res = await getPermissionList();
      this.list = tranListToTreeData(res.data);
    },
    async loadPermissionById() {
      const res = await getRoleDetail(this.roleId);
      console.log("当前角色的id", res);
      // 数据回填
      this.$refs.tree.setCheckedKeys(res.data.permIds);
    },
  },
};
</script>

<style>
</style>